# CONTROL


def is_life(colonias_list, life_ability):
    cells_num = 0
    green_cells_num = 0
    for colonia in colonias_list:
        cells = len(colonia.cells)
        if str(type(colonia)) == "<class 'model.Green_Cells'>":
            green_cells_num = cells
        cells_num += cells

    if green_cells_num / cells_num >= life_ability:
        return True
    else:
        return False
    
def save_situation(colonias_list, iteration): 
    # Write 4 numbers in file: iteration, number of reds, number of greens, number of blues
    # Sequence: R then G then B. (Memory-rule "RGB")
    red_cells_num = 0
    green_cells_num = 0
    blue_cells_num = 0
    
    for colonia in colonias_list:
        if str(type(colonia)) == "<class 'model.Green_Cells'>":
            green_cells_num += len(colonia.cells)
        if str(type(colonia)) == "<class 'model.Red_Cells'>":
            red_cells_num += len(colonia.cells)        
        if str(type(colonia)) == "<class 'model.Blue_Cells'>":
            blue_cells_num += len(colonia.cells)
    data_file = open("data.txt", 'a')
    data_file.write(' '.join(list(map(str, [iteration, red_cells_num, green_cells_num, blue_cells_num]))) + '\n')
    data_file.close()

def is_virus(red_colonia):
    if len(red_colonia.cells) == 0:
        return False
    else:
        return True
    